 //
//  CYCStatusRetweetView.swift
//  微博cyc
//
//  Created by cyc on 16/3/5.
//  Copyright © 2016年 cyc. All rights reserved.
//

import UIKit
import SnapKit
 //被转发的微博内容
class CYCStatusRetweetView: UIView {

    // MARK: - 属性
    private var statusRetweetViewBottom: Constraint?
    
    /// 微博模型,用于显示被转发微博的数据
    var status: CZStatus? {
        didSet {
            // 设置内容
            let name = status?.user?.screen_name ?? "没有名称"
            let text = status?.text ?? "没有微博内容"
            
            retweetContentLabel.text = "@\(name): \(text)"
            
            // 判断是否有图片需要显示
            statusRetweetViewBottom?.uninstall()
            if let _ = status?.storePictureURLs {
                // 有配图需要显示设置配图视图显示内容
                retweetPictureView.hidden = false
                retweetPictureView.status = status
                
                self.snp_makeConstraints { (make) -> Void in
                    self.statusRetweetViewBottom = make.bottom.equalTo(retweetPictureView).offset(CZStatusCellMargin).constraint
                }
            } else {
                //7-8修改
                retweetPictureView.hidden = true
                
                self.snp_makeConstraints { (make) -> Void in
                    self.statusRetweetViewBottom = make.bottom.equalTo(retweetContentLabel).offset(CZStatusCellMargin).constraint
                }
            }
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareUI()
    }
    
    private func prepareUI() {
        // 1.添加控件
        addSubview(retweetBkgButton)
        addSubview(retweetContentLabel)
        addSubview(retweetPictureView)
        
        // 2.添加约束
        // 背景按钮
        retweetBkgButton.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self)
        }
        
        // 转发微博内容
        retweetContentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.top.equalTo(self).offset(CZStatusCellMargin)
            make.trailing.equalTo(self).offset(-CZStatusCellMargin)
        }
        
        // 被转发微博的配图
        retweetPictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(CZStatusCellMargin)
            make.top.equalTo(retweetContentLabel.snp_bottom).offset(CZStatusCellMargin)
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        // CZStatusRetweetView自己的高度根据子控件的内容来
        self.snp_makeConstraints { (make) -> Void in
            self.statusRetweetViewBottom = make.bottom.equalTo(retweetPictureView).offset(CZStatusCellMargin).constraint
        }
    }
    
    
    // MARK: - 懒加载
    /// 背景按钮
    private lazy var retweetBkgButton: UIButton = {
        let button = UIButton()
        
        // 设置
        button.setBackgroundImage(UIImage(named: "retweet_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "retweet_background_highlighted"), forState: UIControlState.Highlighted)
        
        return button
    }()
    
    /// 转发微博内容
    private lazy var retweetContentLabel: UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), font: 15)
        
        label.numberOfLines = 0
        
        return label
    }()
    
    /// 被转发微博的配图
    private lazy var retweetPictureView: CycStatusPrictureView = CycStatusPrictureView()


}
 //目标: 在用户将tableView滚动到最后一个cell的时候,主动去加载更多数据
 //使用 第三方框架 SVPullToRefresh 来实现 在用户将 tableView 滚动到 最后 一个 cell 的时候,主动去加载更多数据
